import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { NzModalRef, NzFormatEmitEvent, NzModalService, NzTreeComponent, NzTreeNodeOptions, NzMessageService } from 'ng-zorro-antd';
import { PagesService } from '../../services/pages.service';
import { UtilService } from '../../services/util.service';
import { RequestService } from '../../services/request.service';
declare var layui: any;
declare var layer: any;
declare var $: any;

@Component({
  selector: 'app-unitc',
  templateUrl: './unitc.component.html',
  styleUrls: ['./unitc.component.scss']
})
export class UnitcComponent implements OnInit {

  constructor(public router: Router, private http: HttpClient, public interfaceService: PagesService, public utilService: UtilService, public request: RequestService) {

  }
  field1 = []     //table表格1-主表列字段集合
  data1 = {}      //table表格1-主表对象数据集合
  data1_dis = {}  //table表格1-主表对象禁用集合
  data1_sel = []  //table表格1-主表下拉框数据集合
  data1_att = []  //table表格1-主表自定义属性数据集合
  @Output() private outer = new EventEmitter();
  @Input() puc_msg: any;

 /**
   * 表格字段动态列集合生成
   */
  filed_tablelist() {
    this.interfaceService.postPage("/erp/common/fields/getfieldsbytype", { "type":"unitOption.OptionVo" }).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      if(data.code==200){
        this.field1=this.utilService.filed_list(data.object,1);
        this.popup01_table();//初始化列表表格数据
      }
    }
    });
  }

  ngOnInit() {
this.filed_tablelist()
  }



  unitStatus_list = JSON.parse(localStorage.getItem("unitStatus_list"))
  popup01_url = '';
  popup01_para
  popup01 = false;
  popup001_show(switchUnitId, type, value): void {//数据验证事件，post传参
    this.interfaceService.getUnitOption(switchUnitId, value, type).subscribe((data: any) => {
      if (this.interfaceService.codeValidation(data)) {
      if (data.code != 200) {
        layer.msg(data.message);
      }
      this.outer.emit(data.object);
    }
    });
  }
  popup01_show(url, para?): void {//弹窗事件，默认get传参，第二参数为post方法传参，格式{"xxx":"xxx"}
    this.popup01_url = url
    this.popup01_para = para;
    this.popup01 = true;
    this.popup01_table();
  }
  popup01_hide(): void {
    this.popup01 = false;
    this.popup01_url = '';
    this.popup01_para = '';
    this.popup01_ss1 = '';
    this.popup01_ss2 = '';
    this.popup01_ss3 = '';
    this.popup01_ss4 = '';
  }

  popup01_data = {}
  popup01_ss1 = '';
  popup01_ss2 = '';
  popup01_ss3 = '';
  popup01_ss4 = '';
  popup01_table() {
    if (typeof (this.popup01_para) == "undefined") {
      this.popup01_listTable(this.popup01_url + "&unitCode=" + this.popup01_ss1 + "&unitName=" + this.popup01_ss2 + "&inputCode=" + this.popup01_ss3 + "&unitStatus=" + this.popup01_ss4);
    } else {
      this.popup01_data = {}
      Object.assign(this.popup01_data, this.popup01_para, { unitCode: this.popup01_ss1, unitName: this.popup01_ss2, inputCode: this.popup01_ss3, unitStatus: this.popup01_ss4 });
      this.interfaceService.postPage(this.popup01_url, this.popup01_para).subscribe((data: any) => {
        if (this.interfaceService.codeValidation(data)) {
        if (data.code == 200) {
          this.popup01_listTable2(data.object.list);
        }
      }
      });
    }
  }
  indexTableId = 'popup01_tblist_unitc'
  popup01_listTable(url): void {
    setTimeout(() => {
      layui.use(['table'], (obj) => {
        var  table = layui.table

        var tab = this.utilService.getTable(url, "#" + this.indexTableId, this.field1)
        tab["title"] = '数据表'
        tab["toolbar"] = '#toolbarIndex2_unitc'
      
   
        table.render(tab);

        //头工具栏事件
        table.on('toolbar(popup01_tblist_unitc)', (obj) => {
          var checkStatus = table.checkStatus(obj.config.id);
          switch (obj.event) {
            case 'getCheckData':
              var data = checkStatus.data;
              // layer.alert(JSON.stringify(data));
              //layer.msg('选中了：'+ data.length + ' 个');
              if (data.length > 0) {
                this.popup01_hide();
                this.outer.emit(data)
              } else {
                layer.msg("请选择数据后操作！")
              }

              break;
          };
        });

        /**
         * 单击行勾选checkbox
         */
        $(document).off("click");
        $(document).on("click", "div[lay-id=popup01_tblist_unitc] table.layui-table tbody tr", function () {
          var index = $(this).attr('data-index');
          var tableBox = $(this).parents('.layui-table-box');
          //存在固定列
          if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
            var tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
          } else {
            var tableDiv = tableBox.find("div[lay-id=popup01_tblist_unitc] .layui-table-main");
          }
          var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
          if (checkCell.length > 0) {
            checkCell.click();
          }
          //选中背景色
          $(this).siblings().css("background-color", "#FFFFFF");
          $(this).css("background-color", "#F2F2F2");
        });

        $(document).on("click", "div[lay-id=popup01_tblist_unitc] td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
          e.stopPropagation();
        });

      });

    }, 50);

  }



  popup01_listTable2(url): void {
    setTimeout(() => {
      layui.use(['table'], (obj) => {
        var  table = layui.table
        var tab = this.utilService.getTable(url, "#" + this.indexTableId, this.field1)
        tab["title"] = '数据表'
        tab["toolbar"] = '#toolbarIndex'
        tab["elem"] = '#toolbarIndex2_unitc'
   
        table.render(tab);

        //头工具栏事件
        table.on('toolbar(popup01_tblist_unitc)', (obj) => {
          var checkStatus = table.checkStatus(obj.config.id);
          switch (obj.event) {
            case 'getCheckData':
              var data = checkStatus.data;
              // layer.alert(JSON.stringify(data));
              //layer.msg('选中了：'+ data.length + ' 个');
              if (data.length > 0) {
                this.popup01_hide();
                this.outer.emit(data)
              } else {
                layer.msg("请选择数据后操作！")
              }

              break;
          };
        });

        /**
         * 单击行勾选checkbox
         */
        $(document).off("click");
        $(document).on("click", "div[lay-id=popup01_tblist_unitc] table.layui-table tbody tr", function () {
          var index = $(this).attr('data-index');
          var tableBox = $(this).parents('.layui-table-box');
          //存在固定列
          if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
            var tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
          } else {
            var tableDiv = tableBox.find("div[lay-id=popup01_tblist_unitc] .layui-table-main");
          }
          var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
          if (checkCell.length > 0) {
            checkCell.click();
          }
          //选中背景色
          $(this).siblings().css("background-color", "#FFFFFF");
          $(this).css("background-color", "#F2F2F2");
        });

        $(document).on("click", "div[lay-id=popup01_tblist_unitc] td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
          e.stopPropagation();
        });

      });

    }, 50);

  }



}
